﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Configuration;
using DataAccess.DatabaseManagement;

namespace DataAccess
{
    public class DatabaseProcesser: IDatabaseClient
    {
        DatabaseManagement.IDatabaseClient dbClient = null;

        public DataSet LoadDataSet(IDataReader reader,string tablename)
        {
            if (reader != null)
            {
                DataSet ds = new DataSet();
                ds.EnforceConstraints = false;
                ds.Load(reader, LoadOption.OverwriteChanges, tablename);
                reader.Close();
                return ds;
            }
            else
            {
                return null;
            }
        }

        public DatabaseProcesser(DataAccess.DatabaseManagement.DatabaseType Type)
        {
            switch (Type)
            {
                case DataAccess.DatabaseManagement.DatabaseType.SqlServer:
                    dbClient = new DatabaseManagement.MSSqlServerDAO();
                    break;
                case DataAccess.DatabaseManagement.DatabaseType.MySQL:
                    dbClient = new DatabaseManagement.MySQLDAO();
                    break;
                default:
                    break;
            }
        }

        public string SelectCommand { get { return dbClient.SelectCommand; } }
        public string UpdateCommand { get { return dbClient.UpdateCommand; } }
        public string DeleteCommand { get { return dbClient.DeleteCommand; } }
        public string InsertCommand { get { return dbClient.InsertCommand; } }

        public bool TransactionIsNull()
        {
            return (dbClient.Transaction == null);
        }

        public void Open()
        {
            dbClient.Open();
        }

        public void Commit()
        {
            dbClient.Commit();
        }

        #region Lấy kết nối CSDL
        void GetConnection()
        {
            dbClient.getConnection();
        } 
        #endregion

        #region Thực thi một câu lệnh select
        public IDataReader ExecuteSelectCommand(string select_cmd)
        {
            return dbClient.ExecuteSelectCommand(select_cmd);
        }        
        #endregion

        #region Thực thi một câu lệnh insert
        public int ExecuteInsertCommand(string insertCmd)
        {
            return dbClient.ExecuteInsertCommand(insertCmd);
        }
        #endregion

        #region Thực thi một câu lệnh insert
        public int ExecuteInsertCommand(string insertCmd, IParamenter[] param, IParamenter[] paramcondition = null)
        {
            return dbClient.ExecuteInsertCommand(insertCmd, param, paramcondition);
        }
        #endregion

        public int ExecuteSelectCommand(string insertCmd, IParamenter[] param, IParamenter[] paramcondition = null)
        {
            return dbClient.ExecuteInsertCommand(insertCmd, param, paramcondition);
        }

        public long GetAutoIncrement(string tablename)
        {
            return dbClient.GetAutoIncrement(tablename);
        }

        #region Đóng kết nối CSDL
        public void CloseConnection(bool unsafeclose = false)
        {
            dbClient.CloseConnection(unsafeclose);
        }   
        #endregion      

    
        public IDbTransaction Transaction
        {
            get
            {
                return dbClient.Transaction;
            }
            set
            {
                dbClient.Transaction = value;
            }
        }

        public IDbConnection Connection
        {
            get
            {
                return dbClient.Connection;
            }
            set
            {
                dbClient.Connection = value;
            }
        }

        public IDbConnection getConnection()
        {
            return dbClient.getConnection();
        }

        public IDataReader ExecuteSelectCommand(string select_cmd, IParamenter[] param)
        {
            return dbClient.ExecuteSelectCommand(select_cmd, param);
        }

        public IDataReader ExecuteSelectCommand(string select_cmd, List<IParamenter> param = null)
        {
            return dbClient.ExecuteSelectCommand(select_cmd, param);
        }

        public int ExecuteInsertCommand(string insertCmd, IParamenter[] param = null)
        {
            return dbClient.ExecuteInsertCommand(insertCmd, param);
        }

        public int ExecuteInsertCommand(string insertCmd, List<IParamenter> param)
        {
            return dbClient.ExecuteInsertCommand(insertCmd, param);
        }

        public void RollBack()
        {
            dbClient.RollBack();
        }

        public List<IParamenter> GetParameter()
        {
            return dbClient.GetParameter();
        }


        IDataReader IDatabaseClient.ExecuteSelectCommand(string select_cmd, IParamenter[] param, IParamenter[] paramcondition = null)
        {
            throw new NotImplementedException();
        }


        public DataSet GetDataSetFormSelectCommand(string select_cmd)
        {
            return dbClient.GetDataSetFormSelectCommand(select_cmd);
        }

        public DataSet GetDataSetFormSelectCommand(string select_cmd, IParamenter[] param)
        {
            return dbClient.GetDataSetFormSelectCommand(select_cmd, param);
        }

        public DataSet GetDataSetFormSelectCommand(string select_cmd, IParamenter[] param, IParamenter[] paramcondition = null)
        {
            return dbClient.GetDataSetFormSelectCommand(select_cmd, param, paramcondition);
        }

        public DataSet GetDataSetFormSelectCommand(string select_cmd, List<IParamenter> param = null)
        {
            return dbClient.GetDataSetFormSelectCommand(select_cmd,param);
        }


        public int ExecuteCreateCommand(string select_cmd)
        {
            return dbClient.ExecuteCreateCommand(select_cmd);
        }
    }
}